﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.DTOs.Configuration;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.web.wrokflows.DTOs;

public partial class Administration_EmailNotification : BaseView, IEmailNotification
{
    private EmailNotificationController _controller;
    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new EmailNotificationController(this);
        _controller.InitView(Page.IsPostBack);
        if (!IsPostBack)
        {
            #region Site Map
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.EmailNotification);
            #endregion
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.HandleSaveNotificationModeStates();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        _controller.InitView(false);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.HandleCancel();
    }

    #region IEmailNotification Members

    public List<NotificationModeInfo> EftNotificationModeList
    {
        get
        {
            return (from ListItem item in chkBoxListNotificationMode.Items
                    where item.Selected
                    select new NotificationModeInfo
                    {
                        NotificationModeId = Convert.ToInt64(item.Value),
                        NotificationInterval = item.Text
                    }).ToList();
        }
        set
        {
            chkBoxListNotificationMode.DataSource = value;
            chkBoxListNotificationMode.DataTextField = "NotificationInterval";
            chkBoxListNotificationMode.DataValueField = "NotificationModeId";
            chkBoxListNotificationMode.DataBind();

            if (EftNotificationStateModeSetList.Count == 0)
            {
                chkBoxListNotificationMode.Items[0].Selected = true;
            }
            else
            {
                foreach (ListItem listItem in chkBoxListNotificationMode.Items)
                {
                    if (EftNotificationStateModeSetList.Where(md => md.NotificationModeId == Convert.ToInt64(listItem.Value)).Count() >= 1)
                        listItem.Selected = true;
                }
            }
        }
    }

    public List<NotificationStateInfo> EftNotificationStateList
    {
        get
        {
            return (from TreeNode treeNode in treeViewNotificationStateList.CheckedNodes
                    select new NotificationStateInfo
                    {
                        NotificationStateId = Convert.ToInt64(treeNode.Value),
                        StateName = treeNode.Text,
                    }).ToList();
        }
        set
        {
            treeViewNotificationStateList.Nodes.Clear();
            var workflowNames = value.Select(w => w.WorkflowName).Distinct().ToList();
            foreach (var workflowName in workflowNames)
            {
                TreeNode parentNode = new TreeNode(workflowName);
                var lstState = value.Where(w => w.WorkflowName.Equals(workflowName)).ToList();
                TreeNode childNode;
                foreach (var state in lstState)
                {
                    childNode = new TreeNode(state.StateName, state.NotificationStateId.ToString());

                    if (EftNotificationStateModeSetList.Where(st => st.NotificationStateId == Convert.ToInt64(state.NotificationStateId)).Count() >= 1)
                    {
                        childNode.Checked = true;
                    }

                    parentNode.ChildNodes.Add(childNode);
                }
                treeViewNotificationStateList.Nodes.Add(parentNode);
            }
            treeViewNotificationStateList.ExpandAll();
        }
    }
    public List<NotificationStateModesInfo> EftNotificationStateModeSetList
    {
        get;
        set;
    }
    public ErrorInformation ErrorDetails
    {
        get { return pnlError.ErrorInformation; }
        set { pnlError.ErrorInformation = value; }
    }
    #endregion

}